package com.cndw.kungfu.model.activity.domain;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.ext.XmlProcesser;

public class CarnivalConfig {
	/**
	 * 编号
	 */
	private int id;
	/**
	 * 几率
	 */
	private int rate;
	/**
	 * 物品id
	 */
	private int goodsId;
	/**
	 * 数量
	 */
	private int num;
	/**
	 * 银两几率
	 */
	private int assetAward;
	/**
	 * 等级
	 */
	private int level;
	/**
	 * 随机奖励
	 */
	private String rateIds;

	private int[] rateIdsArray;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getRate() {
		return rate;
	}

	public void setRate(int rate) {
		this.rate = rate;
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getAssetAward() {
		return assetAward;
	}

	public void setAssetAward(int assetAward) {
		this.assetAward = assetAward;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getRateIds() {
		return rateIds;
	}

	public void setRateIds(String rateIds) {
		this.rateIds = rateIds;
		if (rateIds != null && rateIds.length() > 0) {
			rateIdsArray = StringProcesser.toIntArray(rateIds);
		}
	}

	public int[] getRateIdsArray() {
		return rateIdsArray;
	}

	public void setRateIdsArray(int[] rateIdsArray) {
		this.rateIdsArray = rateIdsArray;
	}

	private static final String configFile = "carnival.xml";

	private static final Map<Integer, CarnivalConfig> commData = new ConcurrentHashMap<Integer, CarnivalConfig>();

	static {
		load();
	}

	public static synchronized void load() {
		String fileName = ConstantsConfig.getLocaleResource() + configFile;
		List<CarnivalConfig> carnivalList = XmlProcesser.processXml(CarnivalConfig.class, fileName);
		int rate = 0;
		for (CarnivalConfig e : carnivalList) {
			rate += e.getRate();
			commData.put(e.getId(), e);
		}
		if (rate != 10000) {
			LogUtil.getMain().error("carnival rate is wrong ..." + rate);
		}
	}

	public static synchronized void clear() {
		load();
	}

	public static Collection<CarnivalConfig> getAll() {
		return commData.values();
	}

}
